package springmaven;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.dlry.web")
// @ImportResource(value = "classpath:springmaven/dwr.xml")
public class WebConfig extends WebMvcConfigurerAdapter {
	// 配置视图解析器
	@Bean
	public TilesViewResolver tilesViewResolver() {
		TilesViewResolver tilesViewResolver = new TilesViewResolver();
		// tilesViewResolver.setOrder(2);
		return tilesViewResolver;
	}
	// @Bean
	// public ViewResolver viewResolver() {
	// InternalResourceViewResolver resolver = new
	// InternalResourceViewResolver();
	// resolver.setPrefix("/WEB-INF/views/");
	// resolver.setSuffix(".jsp");
	// resolver.setOrder(1);
	// resolver.setExposeContextBeansAsAttributes(true);
	// return resolver;
	// }

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	// 处理静态资源
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/META-INF/resources/");
	}

	// 配置tiles解析器设置类
	@Bean
	public TilesConfigurer tilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();
		tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/layout/tiles.xml" });
		tilesConfigurer.setCheckRefresh(true);
		return tilesConfigurer;
	}

//	@Bean
//	public SimpleMappingExceptionResolver exceptionResolver() {
//		SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
//		exceptionResolver.setDefaultErrorView("error");
//		exceptionResolver.setExceptionAttribute("exception");
//		Properties mappings = new Properties();
//		mappings.put(Exception.class, "error_jsp");
//		exceptionResolver.setExceptionMappings(mappings);
//		System.out.println("globe");
//		return exceptionResolver;
//	}

	// @Override
	// public void configureContentNegotiation(ContentNegotiationConfigurer
	// configurer) {
	// configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
	// }
	// @Bean
	// public LocalValidatorFactoryBean localValidatorFactoryBean() {
	// LocalValidatorFactoryBean localValidatorFactoryBean = new
	// LocalValidatorFactoryBean();
	// localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
	// return localValidatorFactoryBean;
	// }
	//
	// @Bean
	// public MultipartResolver multipartResolver() throws Exception {
	// return new StandardServletMultipartResolver();
	// }

}
